﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcKinhdoanhcaycanh.Models;

namespace MvcKinhdoanhcaycanh.Controllers
{
    public class DMSanphamController : Controller
    {
        DataClassesDataContext data = new DataClassesDataContext();

        //
        // GET: /DMSanpham/

        public ActionResult Index()
        {
            var All_sanpham = from sp in data.loaisphams select sp;
            return View(All_sanpham);
        }
        public ActionResult details(int id)
        {
            
            var details_sp = data.loaisphams.Where(m => m.maloai == id).First();
            return View(details_sp);
        }
        public ActionResult create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult create(FormCollection collection, loaispham lsp)
        {
            var cb_loaisp = collection["tenloai"];
            if (string.IsNullOrEmpty(cb_loaisp))
            {
                ViewData["Error"] = "Not allow empty";
            }
            else
            {
                lsp.tenloai = cb_loaisp;
                data.loaisphams.InsertOnSubmit(lsp);
                data.SubmitChanges();
                return RedirectToAction("index");

            }
            return this.create();
        }
        public ActionResult edit(int id)
        {
            var eb_sp = data.loaisphams.First(m => m.maloai == id);
            return View (eb_sp);
        }
        [HttpPost]
        public ActionResult edit(int id, FormCollection collection)
        {
            var lsp = data.loaisphams.First(m => m.maloai == id);
            var e_lspham = collection["tenloai"];
            lsp.maloai = id;
            if (string.IsNullOrEmpty(e_lspham))
            {
                ViewData["Error"] = "not allow empty";
            }
            else
            {
                lsp.tenloai = e_lspham;
                UpdateModel(lsp);
                data.SubmitChanges();
                return RedirectToAction("index");
            }
            return this.edit(id);
        }
        public ActionResult delete(int id)
        {
            var d_sp = data.loaisphams.First(m => m.maloai == id);
            return View(d_sp);
        }
        [HttpPost]
        public ActionResult delete(int id, FormCollection collection)
        {
            var d_sp = data.loaisphams.Where(m => m.maloai == id).First();
            data.loaisphams.DeleteOnSubmit(d_sp);
            data.SubmitChanges();
            return RedirectToAction("index");
        }
    }
}
